﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Net;
using System.Windows.Forms;
using WindowsHelper;

namespace WebUpdate
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public static int InsideRun = Program.RunI;
        private static string SBarPath = Path.GetDirectoryName(Application.ExecutablePath) + @"\SBar.exe";
        private void Form1_Load(object sender, EventArgs e)
        {

//                System.Net.WebClient client = new WebClient();
////                string s = client.DownloadString("http://192.168.10.21:50001/webupdateAPP/Updates/SBAR");
//                client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
//                client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
//
//
//                if (File.Exists(Path.GetTempPath() + @"\Sbar.exe"))
//                {
//                    File.Delete(Path.GetTempPath() + @"\Sbar.exe");
//                }
//                try
//                {
//                    client.DownloadFileAsync(new Uri("http://192.168.10.21:50001/webupdateAPP/Updates/SBAR/SBAR.exe"), Path.GetTempPath() + @"\Sbar.exe");
//                }
//                catch
//                {
//
//                } 
            if (InsideRun == 0)
            {
                this.Location = new Point((Screen.PrimaryScreen.WorkingArea.Width - this.Width) / 2, (Screen.PrimaryScreen.WorkingArea.Height - this.Height) / 2);
            }
        }

        void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            ProgressBar.PerformStep();
        }

        void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(Path.GetTempPath() + @"\Sbar.exe");
            FileVersionInfo fvl =
                FileVersionInfo.GetVersionInfo(SBarPath);
            FileInfo fi = new FileInfo(Path.GetTempPath() + @"\Sbar.exe");
            if (fi.Length != 0)
            {
                if (fvi.FileVersion != fvl.FileVersion)
                {

                    if (InsideRun == 1 | InsideRun == 3)
                    {
                        notifyIcon1.Visible = true;
                        notifyIcon1.ShowBalloonTip(500000000);
                    }
                    else
                    {
                        notifyIcon1.Visible = false;
                        IsUpdate.Text = " New Update found!";
                        btnCheck.Enabled = true;
                    }

                }
                else
                {
                    if (InsideRun == 1 | InsideRun == 3)
                    {
                        Close();
                    }
                    else
                    {
                        IsUpdate.Text = "No updates found!";
                    }
                } 
            }
            else
            {
                IsUpdate.Text = "No updates found!";
                File.Delete(Path.GetTempPath() + @"\Sbar.exe");
            }
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            File.Delete(Path.GetTempPath() + @"\Sbar.exe");
            System.Threading.Thread.Sleep(2000);
            Close();
            
        }

        private void button2_Click(object sender, EventArgs e)
        {

            IntPtr SBarHandle = WindowsAPI.FindWindow((string)null, "SBar Developper Dialog");
            WindowsAPI.PostMessage(SBarHandle, WindowsAPI.WM_SYSCOMMAND, (IntPtr)WindowsAPI.SC_CLOSE, IntPtr.Zero);
            System.Threading.Thread.Sleep(2000);
            File.Copy(Path.GetTempPath() + @"\Sbar.exe", SBarPath, true);
            File.Delete(Path.GetTempPath() + @"\Sbar.exe");
            Process.Start(SBarPath);
            System.Threading.Thread.Sleep(2000);
            Close();
            
        }

        private void notifyIcon1_BalloonTipClicked(object sender, EventArgs e)
        {

            IntPtr SBarHandle = WindowsAPI.FindWindow((string)null, "SBar Developper Dialog");
            WindowsAPI.PostMessage(SBarHandle, WindowsAPI.WM_SYSCOMMAND, (IntPtr)WindowsAPI.SC_CLOSE, IntPtr.Zero);
            System.Threading.Thread.Sleep(2000);
            File.Copy(Path.GetTempPath() + @"\Sbar.exe", SBarPath, true);
            File.Delete(Path.GetTempPath() + @"\Sbar.exe");
            Process.Start(SBarPath);
            System.Threading.Thread.Sleep(2000);
            Close();
            
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            System.Net.WebClient client = new WebClient();
            //                string s = client.DownloadString("http://192.168.10.21:50001/webupdateAPP/Updates/SBAR");
            client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
            client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);


            if (File.Exists(Path.GetTempPath() + @"\Sbar.exe"))
            {
                File.Delete(Path.GetTempPath() + @"\Sbar.exe");
            }
            try
            {
                client.DownloadFileAsync(new Uri("http://192.168.10.21:50001/webupdateAPP/Updates/SBAR/SBAR.exe"), Path.GetTempPath() + @"\Sbar.exe");
            }
            catch
            {

            } 
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Location = new Point((Screen.PrimaryScreen.WorkingArea.Width - this.Width) / 2, (Screen.PrimaryScreen.WorkingArea.Height - this.Height) / 2);
            WindowsAPI.SetWindowPos(this.Handle, WindowsAPI.HWND_TOPMOST, this.Location.X, this.Location.Y, this.Width,
                                    this.Height, WindowsAPI.SWP_SHOWWINDOW);
            
        }
    }
}
